//
//  SignalMovementImageManager.m
//  Portable Traffic Signal Assistor
//
//  Created by Hao Xu on 6/4/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//

#import "SignalMovementImageManager.h"

@implementation SignalMovementImageManager
static const int NORTHBOUND_POSITION_X=0;
static const int NORTHBOUND_POSITION_Y=0;
static const int SOUTHBOUND_POSITION_X=0;
static const int SOUTHBOUND_POSITION_Y=0;
static const int EASTBOUND_POSITION_X=0;
static const int EASTBOUND_POSITION_Y=0;
static const int WESTBOUND_POSITION_X=0;
static const int WESTBOUND_POSITION_Y=0;
static const int NORTHSOUTH_WIDTH=15;
static const int NORTHSOUTH_HEIGHT=24;
static const int WESTEAST_WIDTH=24;
static const int WESTEAST_HEIGHT=15;

@synthesize ring1ImageList=_ring1ImageList;
@synthesize ring2ImageList=_ring2ImageList;

-(void) dealloc
{
    [_ring1ImageList release];
    [_ring2ImageList release];
    [super dealloc];
}

- (MovementImageView *) getDirectionImageViewByMovement:(NSString *)movement
{
    MovementImageView *imageView = [[[MovementImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",movement]]] autorelease];
        return imageView;
}

- (id) initWithSignal:(Signal *)signal
{
    self=[super init];
    self.ring1ImageList=[[[NSMutableArray alloc] init] autorelease];
    self.ring2ImageList=[[[NSMutableArray alloc] init] autorelease];
    for (int i=0; i<signal.ring1.phaseList.count; i++) {
        SignalPhase *phase=[signal.ring1.phaseList objectAtIndex:i];
//        NSString *phaseMovement=phase.phaseMovement;
        
        NSString* phaseMovement=phase.phaseMovement;
        NSMutableArray* movementList=[[[NSMutableArray alloc] init] autorelease];
        NSRange commaRange=[phaseMovement rangeOfString:@","];
        while (commaRange.location!=NSNotFound) {
            NSString *movement=[phaseMovement substringToIndex:commaRange.location];
            [movementList addObject:movement];
            phaseMovement=[phaseMovement substringFromIndex:(commaRange.location+1)];
            commaRange=[phaseMovement rangeOfString:@","];
        }
        if (phaseMovement.length>0) {
            [movementList addObject:phaseMovement];
        }
        
        for (NSString *movement in movementList) {

        
        NSRange range = [movement rangeOfString:@"B"];
        
//        NSString *imageName=nil;
        if (range.location != NSNotFound) {
            // found a colon
            if (range.location + 1 < [movement length]) {
                NSString *directionName =[movement substringToIndex:range.location] ;
                NSString *movementName=[movement substringFromIndex:(range.location + 1)];
                for (int k=0; k<movementName.length; k++) {
                    NSString *movementCharacter=[movementName substringFromIndex:k];
                    movementCharacter=[movementCharacter substringToIndex:1];
                    NSString *imageName=[NSString stringWithFormat:@"%@%@.png",directionName,movementCharacter];
                    MovementImageView *movementView=[[[MovementImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageName]]] autorelease];
                    movementView.phaseID=phase.phaseID;
                    movementView.ringID=@"1";
                    movementView.direction=directionName;
                    movementView.movement=movementCharacter;
                    movementView.imageName=imageName;
                    [movementView setFrameByDirection];
                    [self.ring1ImageList addObject:movementView];
                }
                
//                return nil;
            }else {
//                return nil;
            }
        }
        }

        
    }
    for (int i=0; i<signal.ring2.phaseList.count; i++) {
        SignalPhase *phase=[signal.ring2.phaseList objectAtIndex:i];
//        NSString *phaseMovement=phase.phaseMovement;
        NSString* phaseMovement=phase.phaseMovement;
        NSMutableArray* movementList=[[[NSMutableArray alloc] init] autorelease];
        NSRange commaRange=[phaseMovement rangeOfString:@","];
        while (commaRange.location!=NSNotFound) {
            NSString *movement=[phaseMovement substringToIndex:commaRange.location];
            [movementList addObject:movement];
            phaseMovement=[phaseMovement substringFromIndex:(commaRange.location+1)];
            commaRange=[phaseMovement rangeOfString:@","];
        }
        if (phaseMovement.length>0) {
            [movementList addObject:phaseMovement];
        }
        
        for (NSString *movement in movementList) {

        
        NSRange range = [movement rangeOfString:@"B"];
        
        //        NSString *imageName=nil;
        if (range.location != NSNotFound) {
            // found a colon
            if (range.location + 1 < [movement length]) {
                NSString *directionName =[movement substringToIndex:range.location] ;
                NSString *movementName=[movement substringFromIndex:(range.location + 1)];
                for (int k=0; k<movementName.length; k++) {
                    NSString *movementCharacter=[movementName substringFromIndex:k];
                    movementCharacter=[movementCharacter substringToIndex:1];
                    NSString *imageName=[NSString stringWithFormat:@"%@%@.png",directionName,movementCharacter];
                    MovementImageView *movementView=[[[MovementImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageName]]] autorelease];
                    movementView.phaseID=phase.phaseID;
                    movementView.ringID=@"2";
                    movementView.direction=directionName;
                    movementView.movement=movementCharacter;
                    movementView.imageName=imageName;
                    [movementView setFrameByDirection];
                    [self.ring2ImageList addObject:movementView];
                }
                
//                return nil;
            }else {
//                return nil;
            }
        }
        }
        
        
    }

    return self;
}

- (NSArray *) getMovementImageViewsOfPhase:(SignalPhase *)phase
{
    NSMutableArray *imageViewList=[[[NSMutableArray alloc] init] autorelease];
    NSString* phaseMovement=phase.phaseMovement;
    NSMutableArray* movementList=[[[NSMutableArray alloc] init] autorelease];
    NSRange commaRange=[phaseMovement rangeOfString:@","];
    while (commaRange.location!=NSNotFound) {
        NSString *movement=[phaseMovement substringToIndex:commaRange.location];
        [movementList addObject:movement];
        phaseMovement=[phaseMovement substringFromIndex:(commaRange.location+1)];
        commaRange=[phaseMovement rangeOfString:@","];
    }
    if (phaseMovement.length>0) {
        [movementList addObject:phaseMovement];
    }
    
    for (NSString *movement in movementList) {
        NSRange range = [movement rangeOfString:@"B"];
        NSString *directionName =[movement substringToIndex:range.location] ;

        NSString *movementName=[movement substringFromIndex:(range.location + 1)];
        
        for (int k=0; k<movementName.length; k++) {
            NSString *movementCharacter=[movementName substringFromIndex:k];
            movementCharacter=[movementCharacter substringToIndex:1];
            NSString *imageName=[NSString stringWithFormat:@"%@%@.png",directionName,movementCharacter];
            MovementImageView *movementView=[[[MovementImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"GREEN_%@",imageName]]] autorelease];
            movementView.phaseID=phase.phaseID;
            movementView.ringID=@"1";
            movementView.direction=directionName;
            movementView.movement=movementCharacter;
            movementView.imageName=imageName;
            //        [movementView setFrameByDirection];
            {
                if (!movementView.direction||!movementView.movement) {
                    continue;
                }
                NSString *directionMovement=[NSString stringWithFormat:@"%@%@",movementView.direction,movementView.movement];
                if ([directionMovement isEqualToString:@"NL"]) {
                    CGRect imageViewRect = CGRectMake(NORTHBOUND_POSITION_X,  NORTHBOUND_POSITION_Y, NORTHSOUTH_WIDTH , NORTHSOUTH_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }else if ([directionMovement isEqualToString:@"NT"]) {
                    CGRect imageViewRect = CGRectMake(NORTHBOUND_POSITION_X+NORTHSOUTH_WIDTH,  NORTHBOUND_POSITION_Y, NORTHSOUTH_WIDTH , NORTHSOUTH_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }else if ([directionMovement isEqualToString:@"NR"]) {
                    CGRect imageViewRect = CGRectMake(NORTHBOUND_POSITION_X+NORTHSOUTH_WIDTH*2,  NORTHBOUND_POSITION_Y, NORTHSOUTH_WIDTH , NORTHSOUTH_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }
                else if ([directionMovement isEqualToString:@"SL"]) {
                    CGRect imageViewRect = CGRectMake(SOUTHBOUND_POSITION_X+NORTHSOUTH_WIDTH*2,  SOUTHBOUND_POSITION_Y, NORTHSOUTH_WIDTH , NORTHSOUTH_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }else if ([directionMovement isEqualToString:@"ST"]) {
                    CGRect imageViewRect = CGRectMake(SOUTHBOUND_POSITION_X+NORTHSOUTH_WIDTH,  SOUTHBOUND_POSITION_Y, NORTHSOUTH_WIDTH , NORTHSOUTH_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }else if ([directionMovement isEqualToString:@"SR"]) {
                    CGRect imageViewRect = CGRectMake(SOUTHBOUND_POSITION_X,  SOUTHBOUND_POSITION_Y, NORTHSOUTH_WIDTH , NORTHSOUTH_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }
                else if ([directionMovement isEqualToString:@"EL"]) {
                    CGRect imageViewRect = CGRectMake(EASTBOUND_POSITION_X ,  EASTBOUND_POSITION_Y, WESTEAST_WIDTH , WESTEAST_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }else if ([directionMovement isEqualToString:@"ET"]) {
                    CGRect imageViewRect = CGRectMake(EASTBOUND_POSITION_X,  EASTBOUND_POSITION_Y+WESTEAST_HEIGHT, WESTEAST_WIDTH , WESTEAST_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }else if ([directionMovement isEqualToString:@"ER"]) {
                    CGRect imageViewRect = CGRectMake(EASTBOUND_POSITION_X,  EASTBOUND_POSITION_Y+WESTEAST_HEIGHT*2, WESTEAST_WIDTH , WESTEAST_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }
                else if ([directionMovement isEqualToString:@"WR"]) {
                    CGRect imageViewRect = CGRectMake(WESTBOUND_POSITION_X ,  WESTBOUND_POSITION_Y, WESTEAST_WIDTH , WESTEAST_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }else if ([directionMovement isEqualToString:@"WT"]) {
                    CGRect imageViewRect = CGRectMake(WESTBOUND_POSITION_X,  WESTBOUND_POSITION_Y, WESTEAST_WIDTH , WESTEAST_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }else if ([directionMovement isEqualToString:@"WL"]) {
                    CGRect imageViewRect = CGRectMake(WESTBOUND_POSITION_X,  WESTBOUND_POSITION_Y+WESTEAST_HEIGHT, WESTEAST_WIDTH , WESTEAST_HEIGHT);
                    movementView.frame = imageViewRect;
                    movementView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
                }
                
            }
            [imageViewList addObject:movementView];
        }
    }
    
//    NSRange range = [phaseMovement rangeOfString:@"B"];
//    NSString *directionName =[phaseMovement substringToIndex:range.location] ;
//
//    NSString *movementName=[phaseMovement substringFromIndex:(range.location + 1)];
    
    

    
    return imageViewList;
}

- (void) setPhaseImageColor:(NSString *) phaseID ring:(int)ring color:(int) color;
{
    switch (ring) {
        case 1:
        {
            switch (color) {
                case GREEN:
                {
                    for (MovementImageView* imageView in self.ring1ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"GREEN_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                    
                }
                    break;
                case YELLOW:
                {
                    for (MovementImageView* imageView in self.ring1ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"YELLOW_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                    
                    
                }
                    break;
                case RED:
                {
                    for (MovementImageView* imageView in self.ring1ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                    
                    
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 2:
        {
            switch (color) {
                case GREEN:
                {
                    
                    for (MovementImageView* imageView in self.ring2ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"GREEN_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                }
                    break;
                case YELLOW:
                {
                                        for (MovementImageView* imageView in self.ring2ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"YELLOW_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                    
                }
                    break;
                case RED:
                {
                    for (MovementImageView* imageView in self.ring2ImageList) {
                        if ([imageView.phaseID isEqualToString:phaseID])
                        {
                            imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                            continue;
                            //                    return;
                        }
                        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"RED_%@",imageView.imageName]];
                    }
                    
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
            
        default:
            break;
    }
    
}
@end
